package classroom;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public class Teacher extends People {
    protected String subject;
    private final Map<String, Integer> answer2count = new HashMap<>();

    public Teacher(int age, String name, String words, String expression) {
        this(age, name, words, expression, "高数");
    }

    public Teacher(int age, String name, String words, String expression, String subject) {
        super(age, name, words, expression);
        this.subject = subject;
    }

    @Override
    protected String words() {
        return words + "。今天我给大家讲一讲《" +
                subject +
                "》";
    }

    @Override
    protected String action() {
        return name + "老师" +
                action + "。";
    }

    @Override
    public void answerQuestion(String answer) {
        System.out.println("A--Teacher " + name + " :");
        if (answer == null) {
            System.out.println("我也不知道");
            return;
        }
        Integer count = answer2count.get(answer);
        if (count != null && count > 1) {
            System.out.println("这道题我已经讲过" + count + "次了！");
        }
        System.out.println("这道题的答案是：" + answer);
        answer2count.merge(answer, 1,
                //匿名内部类
                new BiFunction<Integer, Integer, Integer>() {
                    @Override
                    public Integer apply(Integer integer, Integer integer2) {
                        return integer + integer2;
                    }
                });
        //Lambda表达式
//        answer2count.merge(answer, 1,
//                (integer, integer2) -> integer + integer2);
        //使用相同的函数代替Lambda表达式
//        answer2count.merge(answer, 1,
//                Integer::sum);


    }

    @Override
    public void askQuestion(Answerable answerable, String question) {
        System.out.println("Q--Teacher " + name + " :");
        System.out.println("问题是：" + question);
        //通知那个人（answerable）该回答问题了。
        answerable.answerQuestion(Classroom.getAnswer(question));
    }
}
